package com.MSofLectures.logistic;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.MSofLectures.Entity.Lecture.Lecture;
import com.MSofLectures.Entity.Lecture.LectureImp;

public class GeneralUser extends Basic {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title GeneralUser.java
	 * @Package com.logistic
	 * @author ufo 
	 * 处理一般用户功能
	 */

	private String[] userId;
	private String[] userPwd;// 必须在传过来之前用sha1加密下，加密方式参考platform这个项目
	private String[] newPwd;
	private String[] oldPwd;

	private String[] user_keyName;
	private String[] user_keyValue;
	private String[] max;
	private String[] first;
	private String[] lectureId;

	public String login() {
		try {
			this.user = this.getOperatorOnUser().loadUser(getUserId()[0],
					getUserPwd()[0]);
			if (null != this.user) {
				addUserToSession(this.user.getUserId(), this.user.getUserPwd());
				addUserPriToSeession(this.user
						.getValue("privilegeToCreateLecture"));
				result.put("pri",
						this.user.getValue("privilegeToCreateLecture"));

				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String logout() {
		this.request.getSession().invalidate();
		result.put("flag", "true");
		return SUCCESS;
	}

	public String addUser_key() {
		try {
			if (isOnline()) {
				addSessionToUser();
				for (int i = 0; i < getUser_keyName().length; i++)
					this.user.addKey(getUser_keyName()[i],
							getUser_keyValue()[i]);
				this.user.save();
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String altUser_keyValue() {
		try {
			if (isOnline()) {
				this.user = this.getOperatorOnUser().loadUid(getUserId()[0]);
				for (int i = 0; i < getUser_keyName().length; i++)
					this.user.altValue(getUser_keyName()[i],
							getUser_keyValue()[i]);
				this.user.save();

				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String altUser_Pwd() {
		try {
			if (isOnline()) {
				addSessionToUser();

				for (int i = 0; i < getNewPwd().length; i++)
					if (this.user.getUserPwd().equals(getOldPwd()[i])) {
						this.user.altPwd(getNewPwd()[i]);
						result.put("flag", "true");
					} else
						result.put("flag", "原密码不正确");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String altOneUser_Pwd() {
		try {
			if (isOnline()) {
				this.user = this.getOperatorOnUser().loadUid(getUserId()[0]);
				for (int i = 0; i < getNewPwd().length; i++)
					this.user.altPwd(getNewPwd()[i]);
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String iteratorUser_keys() {// 枚举用户的各种属性
		try {
			if (isOnline()) {
				addSessionToUser();
				this.result = this.user.iterator();
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String showUser_key() {// 展示用户指定属性，可以展示多个
		try {
			if (isOnline()) {
				addSessionToUser();
				int i = 0;
				for (; i < this.getUser_keyName().length; i++) {
					result.put(this.getUser_keyName()[i],
							this.user.getValue(this.getUser_keyName()[i]));
				}
				result.put("flag", "true");
				result.put("uid", this.user.getUserId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String showOneUser() {// 展示指定用户的属性
		try {
			if (isOnline()) {
				this.user = this.getOperatorOnUser().loadUid(getUserId()[0]);
				int i = 0;
				for (; i < getUser_keyName().length; i++)
					result.put(getUser_keyName()[i],
							this.user.getValue(getUser_keyName()[i]));
				result.put("flag", "true");
				result.put("uid", this.user.getUserId());
				result.put("upwd", this.user.getUserPwd());

			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String register() {
		try {
			if (this.getOperatorOnUser().isExist(getUserId()[0])) {
				result.put("errormessage", "true");
			}
			else {
				this.user = this.getOperatorOnUser().newUser(getUserId()[0],
						getUserPwd()[0]);
				login();
				result.put("flag", "true");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String iteratorLectures() {
		try {
			addSessionToUser();
			List<?> list = this.getOperatorOnLecture().iteratorLectures(
					Integer.parseInt(getMax()[0]),
					Integer.parseInt(getFirst()[0]));
			Iterator<?> i = list.iterator();
			int j = 0;
			for (; i.hasNext(); j++)
				result.put("LectureId" + j, i.next().toString());
			if (j > 0) {
				result.put("amountOfLectures", this.getOperatorOnLecture()
						.countLectures().toString());
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String viewLecturesOutline() {
		addSessionToUser();
		LectureImp lecture = new Lecture(getLectureId()[0]);
		Map<String, String> target = lecture.iterator();
		Set<String> set = target.keySet();
		for (Iterator<String> i = set.iterator(); i.hasNext();) {
			String tmp = i.next().toString();
			result.put(tmp, target.get(tmp));
		}
		String j = this.getOperatorOnLecture().countUsers(getLectureId()[0])
				.toString();
		String k = this.getOperatorOnLecture()
				.countcheckUsers(getLectureId()[0]).toString();
		result.put("uname", this.user.getValue("userName"));
		result.put("lectureId", getLectureId()[0]);
		result.put("registerNum", j);
		result.put("checkNum", k);
		result.put("flag", set.size() > 0 ? "true" : "false");
		try {
			lecture.finalize();
		} catch (Throwable e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return SUCCESS;
	}

	@Override
	public Map<String, String> getResult() {
		// TODO 自动生成的方法存根
		return this.result;
	}

	public String[] getUserId() {
		return userId;
	}

	public void setUserId(String[] userId) {
		this.userId = userId;
	}

	public String[] getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String[] userPwd) {
		this.userPwd = userPwd;
	}

	public String[] getUser_keyName() {
		return user_keyName;
	}

	public void setUser_keyName(String[] user_keyName) {
		this.user_keyName = user_keyName;
	}

	public String[] getUser_keyValue() {
		return user_keyValue;
	}

	public void setUser_keyValue(String[] user_keyValue) {
		this.user_keyValue = user_keyValue;
	}

	public String[] getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String[] oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String[] getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String[] newPwd) {
		this.newPwd = newPwd;
	}

	public String[] getLectureId() {
		return lectureId;
	}

	public void setLectureId(String[] lectureId) {
		this.lectureId = lectureId;
	}

	public String[] getMax() {
		return max;
	}

	public void setMax(String[] max) {
		this.max = max;
	}

	public String[] getFirst() {
		return first;
	}

	public void setFirst(String[] first) {
		this.first = first;
	}

}
